<?php
/**
 * Created by PhpStorm.
 * User: eugene
 * Date: 2018/3/19
 * Time: 13:58
 */

namespace App\Transformers;


use App\Models\MyCar;
use App\Models\MyStage;
use App\Repos\CarRepo;
use Carbon\Carbon;

class RecordTransformer extends Transformer
{
    public function transform($item)
    {
        $data = [
            'id'    => $item->car->CarID,
            'car_id'    => $item->car->CarID,
            'name'  => $item->car->CarName,
            'thumb' => $item->car->CarImgBig,
            'color'                 => $item->car->CarColor,
            'income_per_hour'       => $item->car->CarCreditHour,
            'car_sell_price'=>$item->car->CarPrice,
            'items'=>null,
            'predict_income'        => 0,
            'current_income'        => 0,
            'parking_end_timestamp' => 0,
            'parking_end_time' => '',
            'is_mine' => 0,
            'owner'                 => (new MemberTransformer())->setTransformer('forSimple')->singleData($item->carOwner),
        ];
        
        //计算此车的相关属性
        $carRepo = new CarRepo();
        $carRepo->countStatus($item->myCar);
        
        $data['status'] = $carRepo->status;
        $data['status_text'] = $carRepo->status_text;
        $data['current_income'] = $carRepo->current_income;
        $data['predict_income'] = $carRepo->predict_income;
        
        $carbon_end = Carbon::parse($carRepo->parking_end_time);
        if ($carbon_end->isFuture()) {
            $data['parking_end_timestamp'] = $carbon_end->timestamp - Carbon::now()->timestamp;
            $data['parking_end_time'] = $carbon_end->toTimeString();
        }
        //如果是自己
        
        if($carRepo->status <= 3){
            $stage = MyStage::where('uid',$item->carOwner->id)->where('car_id',$item->myCar->ID)->with('stage')->get();
            if($stage){
                $data['items'] = $stage->transform(function($item){
                    return (new StageTransformer())->singleData($item->stage);
                });
            }
        }
    
        $data['id'] = $item->myCar->ID;
        
        return $data;
    }
}